package models

import (
	"gorm.io/gorm"
	"servers/basic/config"
	"time"
)

type User struct {
	Id        uint           `gorm:"column:id;type:bigint;primaryKey;not null;" json:"id"`
	Username  string         `gorm:"column:username;type:varchar(50);not null;" json:"username"`
	Password  string         `gorm:"column:password;type:varchar(255);not null;" json:"password"`
	Mobile    string         `gorm:"column:mobile;type:char(11);not null;" json:"mobile"`
	Nickname  string         `gorm:"column:nickname;type:varchar(255);comment:昵称;not null;" json:"nickname"` // 昵称
	HendImg   string         `gorm:"column:hend_img;type:varchar(255);comment:头像;not null;" json:"hend_img"` // 头像
	Sex       string         `gorm:"column:sex;type:varchar(20);comment:性别;not null;" json:"sex"`            // 性别
	CreatedAt time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt time.Time      `gorm:"column:updated_at;type:datetime(3);not null;" json:"updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (u *User) TableName() string {
	return "user"
}
func (u *User) GetUserByMobile() error {
	return config.DB.Where("mobile = ?", u.Mobile).Limit(1).Find(&u).Error
}
func (u *User) GetUserById() error {
	return config.DB.Where("id = ?", u.Id).Limit(1).Find(&u).Error
}

// 注册
func (u *User) CreateUser() error {
	return config.DB.Create(&u).Error
}
